﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SVAP
{
    public partial class ConfigAlgorithmForm : Form
    {
        private static string strExePath = "";

        public ConfigAlgorithmForm()
        {
            InitializeComponent();
        }

        //确定
        private void btn_ok_Click(object sender, EventArgs e)
        {
            //算法选择，获取所有选中的items，但是由于设置了智能选中一个，所以只能获得一个
            for (int x = 0; x < checkedListBox_Algorithm.CheckedItems.Count; x++)
            {
                string strSelectedItem = checkedListBox_Algorithm.CheckedItems[x].ToString();
                //分词，获得DLL文件名
                string[] strSplit = strSelectedItem.Split(new char[] { ' ' });
                string strFromPath = strExePath + @"Algorithm\" + strSplit[0].ToString();
                string strToPath = strExePath + @"CurrentAlgorithm\RoundRec.dll";

                // 把指定的算法文件从备份中拷贝到当前文件夹中，以便调用
                File.Delete(strToPath);
                File.Copy(strFromPath, strToPath);
            }

            pictureMDI pm = new pictureMDI();
            pm = (pictureMDI)this.Owner;

            //结果配置选择，获取所有选中的items，保存到list中
            for (int x = 0; x < checkedListBox_Config.CheckedItems.Count; x++)
            {
                string strSelectedItem = checkedListBox_Config.CheckedItems[x].ToString();
                pm.strList.Add(strSelectedItem);
            }

            for (int i = 0; i <= (checkedListBox_Config.Items.Count - 1); i++)
            {
                if (checkedListBox_Config.GetItemChecked(i)&&i==0)
                {
                    pm.flag1 = true;
                }
                if (checkedListBox_Config.GetItemChecked(i)&&i==1)
                {
                    pm.flag2 = true;
                }
                if (checkedListBox_Config.GetItemChecked(i)&&(i!=0&&i!=1))
                {
                    pm.flag3 = true;
                }
            }

            if (checkedListBox_Algorithm.SelectedItems.Count > 0 && checkedListBox_Config.SelectedItems.Count > 0)
            {
                pm.isConfigure = true;//更改配置与否的标志
                this.Close();
            }
            else
            {
                MessageBox.Show("请配置算法与输出结果！");
            }
            //this.Close();
        }

        //取消
        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ConfigAlgorithmForm_Load(object sender, EventArgs e)
        {
            //获得".exe"所在路径
            strExePath = System.AppDomain.CurrentDomain.BaseDirectory;
            //从配置文件中读取已经添加的算法列表
            string strFromPath = System.AppDomain.CurrentDomain.BaseDirectory;
            strFromPath += @"\Config.txt";
            StreamReader sr = new StreamReader(strFromPath, Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null && sLine != "")
                    checkedListBox_Algorithm.Items.Add(sLine);
            }
            sr.Close();

            //结果配置的初始化
            string strResultConfig = strExePath + @"\ResultConfig.txt";
            StreamReader sr1 = new StreamReader(strResultConfig, System.Text.Encoding.Default);
            string sLine1 = "";
            while (sLine1 != null)
            {
                sLine1 = sr1.ReadLine();
                if (sLine1 != null)
                    checkedListBox_Config.Items.Add(sLine1);
            }
            sr1.Close();
        }

        private void checkedListBox_Algorithm_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.CurrentValue == CheckState.Checked) return;
            for (int i = 0; i < ((CheckedListBox)sender).Items.Count; i++)
            {
                ((CheckedListBox)sender).SetItemChecked(i, false);
            }
            e.NewValue = CheckState.Checked;
        }
    }
}
